package reflect;

import java.lang.reflect.Method;
import java.util.Scanner;

public class ReflectDemo4 {
    public static void main(String[] args) throws Exception{
        Person p = new Person();
        p.sayHello();
        String returnName = p.getName();
        System.out.println("返回值"+returnName);
        //反射机制实例化对象
        Class cls = Class.forName("reflect.Person");
        Object obj = cls.newInstance();
        //反射机制调用方法

        Method method = cls.getMethod("say");
        method.invoke(obj);
        Method method1 = cls.getMethod("getName");
        Object returnValue = method1.invoke(obj);
        System.out.println("返回值"+returnValue);

        Scanner scanner = new Scanner(System.in);
        System.out.println("请输入类名");
        String className = scanner.nextLine();
        System.out.println("请输入方法名");
        String methodName = scanner.nextLine();
        Class cla = Class.forName(className);
        Object object = cla.newInstance();
        Method met = cla.getMethod(methodName);
        met.invoke(object);


    }
}
